// pages/order/orderDetail/orderDetail.js
const app = getApp();
const data = require("../../../data/mockData.js");
const user = wx.getStorageSync("user");
const order = wx.getStorageSync("order");
const coupon = wx.getStorageSync("coupon");
const driver = wx.getStorageSync("driver");
const evaluate = wx.getStorageSync("evaluate");
const server = wx.getStorageSync("server");
const handler = require("../../../utils/function.js");
var api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        order: wx.getStorageSync("orderDetail"),
        driver: {},
        order_no: '',
        is_pay: 0,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('获取订单号', options)
        this.setData({
            order_no: options.order_no
        })
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        app.request({
            url: api.order.order_detail,
            method: 'post',
            data: {
                order_no: that.data.order_no
            },
            success: function(rs) {
                console.log(rs.data)
                if (rs.code == 1) {
                    that.setData({
                        order: rs.data,
                        phone: rs.data.driver.work_phone,
                        is_pay: rs.data.orderdetail.is_pay
                    });
                }
            }
        })
        /*this.setData({ order: wx.getStorageSync("orderDetail")});
        console.log(this.data.order.driver_ids);
        for (var i = 0; i < driver.length;i++){
          if (driver[i].id === this.data.order.driver_ids[0]){
            this.setData({ driver: driver[i]});
            wx.setStorageSync("driverDetail", driver[i]);
          }
        }
        for (var i = 0; i < coupon.length; i++) {
          if (coupon[i].id === this.data.order.coupon_ids[0]) {
            console.log(coupon[i]);
            wx.setStorageSync("couponDetail", coupon[i]);
          }
        }*/
    },
    // 页面跳转
    skipPageHandler: function(e) {
      
      if (this.data.is_pay == 0 && this.data.order.orderdetail.apply_delete == 0) {
            wx.showToast({
                title: "该订单还未完成",
                icon: 'none',
                duration: 2000
            })
      } else if (this.data.order.orderdetail.apply_delete == 1) {
        wx.showToast({
          title: "该订单已取消",
          icon: 'none',
          duration: 2000
        })
        return;
      } else {
        console.log(e.currentTarget.dataset.skip);
            wx.navigateTo({
                url: e.currentTarget.dataset.skip,
            })
        }
    },
    callPhoneHandler: function() {
        handler.callPhoneHandler(this.data.phone);
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    // console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})